<?php

namespace App\Model\Dao;

use App\Concern\BaseDao;
use Hyperf\Database\ConnectionInterface;
use Hyperf\Database\Query\Builder;
use Hyperf\DbConnection\Db;
use Hyperf\Utils\Arr;

/**
 * TradeOrderFrozenDao
 */
class TradeOrderFrozenDao extends BaseDao {

    /**
     * 表名常量
     * 联表的时候通过XxxDao::TABLE获取
     */
    const TABLE = "trade_order_frozen";

    // 不支持软删除
    protected $softDelete = null;

    /**
     * 父类中通过静态函数获取表名, 因为不能通过静态常量获取, 这个函数是固定写在子类中的
     */
    public static function table (): string {
        return self::TABLE;
    }

    protected function _buildConnectionTable() : ConnectionInterface{
        return Db::connection();
    }

    public function getSumByHandleStatusAndStartTimeAndEndTime($mchId = '', $channelId = '', $handleStatus = [], $startTime, $endTime) {

        $main = sprintf("%s AS main", self::TABLE);
        $order = sprintf("%s AS order", TradeOrderDetailDao::TABLE);

        $builder = $this->_buildConnectionTable()->table($main);
        $builder->leftJoin($order, "order.id", "main.orderId");

        !empty($mchId) && $builder->where("main.mchId", $mchId);
        !empty($channelId) && $builder->where("order.channelId", $channelId);
        $builder->where("main.settleTime", ">=", $startTime);
        $builder->where("main.settleTime", "<", $endTime);

        if (!empty($transStatus)) {
            $builder->whereIn("main.handleStatus", $handleStatus);
        }

        $builder->selectRaw(join(",", [
            "IFNULL(SUM(main.transAmount), 0) AS transAmount",
            "IFNULL(SUM(main.transSettleAmount), 0) AS transSettleAmount",
            "IFNULL(COUNT(main.id), 0) AS transCount",
        ]));

        $info = $builder->groupBy("order.channelId")->first();

        return $info;
    }

    /**
     * 排序
     *
     * @param Builder $builder
     * @param array   $params
     *
     * @return Builder|mixed
     */
    protected function _buildOrder (Builder $builder, $params = []) {

        // 通过前端提交的参数排序
        if (Arr::exists($params, "orderBy")) {

            [$prop, $order] = $params["orderBy"];

            if (in_array($order, ["asc", "desc"])) {

                //switch ($prop) {
                //    case "yieldMonth":
                //        $builder->orderBy("main.yieldMonth", $order);
                //        break;
                //    default:
                //        break;
                //}
            }
        }

        // 如果友其他固定的排序需求, 在这里写, 例如按照时间降序
        $builder->orderBy("createTime", "desc");

        // 追加父类的排序, 父类按照ID升序, 保证排序结果唯一
        return parent::_buildOrder($builder, $params);
    }

    /**
     * 查询条件
     *
     * @param $params
     *
     * @return Builder
     */
    protected function _buildQueryString ($params) {

        // 本表一律命名为main
        $main = sprintf("%s AS main", self::TABLE);

        // 联表表明获取
        //$country = sprintf("%s AS country", CountryDao::TABLE);

        $builder = DB::table($main);

        // 通过 Arr::exists 来判断是否存在该键, isset不准确
        //(Arr::exists($params, "factoryName") && !empty($params["factoryName"])) && $builder->where("main.factoryName", "like", "%{$params['factoryName']}%");
        Arr::exists($params, "createTimeRange")  && $builder->whereBetween('main.createTime',$params['createTimeRange']);
        return $builder;
    }

    
}
